
//头文件
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

#include "DHT.h"
#define DHTPIN 13    // what digital pin we're connected to 
#define DHTTYPE DHT11
//定义引脚变量等
const int  echo=11;                          //echO接D3脚
const int  trig=12;                          //echO接D2脚
const int  echo2=7;                          //echO接D3脚
const int  trig2=8;                          //echO接D2脚
float      distance,distance2,distance3,distance4,distance5;

int i,n,op;
int dd=0,cc=0;
float h,t,f;

LiquidCrystal_I2C lcd(0x27,16,2);  
DHT dht(DHTPIN, DHTTYPE);
Servo myservo;
void MyPrintLCD(String MyString)
{
  for (int i=0;i<MyString.length();i++)
  lcd.write(MyString.charAt(i));
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  dht.begin();
  lcd.init(); 
  lcd.backlight(); 
  pinMode(9,OUTPUT);
  digitalWrite(9,LOW);
  myservo.attach(10);//前舵机
  myservo.write(100);
//  pinMode(10,OUTPUT);
//  pinMode(9,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);//电机驱动
  pinMode(echo,INPUT);                       //设置echo为输入脚
  pinMode(trig,OUTPUT);                      //设置trig为输出脚
  pinMode(echo2,INPUT);                      //设置echo2为输入脚
  pinMode(trig2,OUTPUT);//超声波              //设置trig2为输出脚
  
}

void loop() //循环
{
  
  receiver();
  cc++;
  if(cc>50){csb();cc=0;}
  dd++;
  if(dd>20){dht11q();dd=0;}
  distancedisplay();
  if(op==1){shut3();shut3();shut3();}
  
}
void receiver()//信号接受处理
{
 if(Serial.available())//接受串口信号
 {
    i=Serial.read();//信号赋值
     motor(i);
 }

// if(i=='a')Serial.print("get a\n");
// if(i=='b')Serial.print("get b\n");
// if(i=='c')Serial.print("get c\n");
// if(i=='d')Serial.print("get d\n");
// if(i=='e')//部署操作
// ;
// if(i=='f')//查看信息
// ;
}
void distancedisplay()//每2s左右显示超声波距离
{
   if(n>8)
   {
   lcd.setCursor(10,0);
   lcd.print(distance); //前方距离
   lcd.setCursor(10,1);
   lcd.print(distance2);//后方距离
   n=0;
   }
   n++;
}

void test()//测试电机
{
  digitalWrite(3,LOW);  //停止
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  delay(100);
  digitalWrite(3,LOW);  //前进
  digitalWrite(4,HIGH);
  digitalWrite(5,LOW);
  digitalWrite(6,HIGH);
  delay(100);
  digitalWrite(3,LOW);  //停止
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  delay(100);
  digitalWrite(3,HIGH); //后退
  digitalWrite(4,LOW);
  digitalWrite(5,HIGH);
  digitalWrite(6,LOW);
  delay(100);
}
void motor(int i)
{
//  Serial.println("receive!");
  if(i=='1'||i=='2'||i=='3'||i=='4')
  {
      digitalWrite(3,LOW);//制动
      digitalWrite(4,LOW);
      digitalWrite(5,LOW);
      digitalWrite(6,LOW);
  }
  switch(i)
  {
    case '1':
    {
      digitalWrite(3,HIGH); //前进
      digitalWrite(4,LOW);
      delay(10); 
      digitalWrite(5,LOW);
      digitalWrite(6,HIGH);
      delay(10);
      break;
    }
    case '2':
    {
      digitalWrite(3,LOW);  //右转
      digitalWrite(4,HIGH);
      digitalWrite(5,LOW);  //右转
      digitalWrite(6,HIGH);
      delay(10);
      break;
    }
    case '3':
    {
      digitalWrite(3,LOW); //后退
      digitalWrite(4,HIGH);
      digitalWrite(5,HIGH);
      digitalWrite(6,LOW);
      delay(10);
      break;
    }
    case '4':
    {
      digitalWrite(3,HIGH);//左转
      digitalWrite(4,LOW);
      digitalWrite(5,HIGH);
      digitalWrite(6,LOW);
      delay(10);
      break;
    }
    case '5':
    {
      digitalWrite(3,LOW);  //停止
      digitalWrite(4,LOW);
      digitalWrite(5,LOW);
      digitalWrite(6,LOW);
      delay(10);
      break;
    }
    case '6'://回传数据
    {
      delay(200);
      String sdata = "H:"+ String(h) +"\n"+"T:"+ String(t) +"\n"+"D1:"+String(distance)+"\n"+"D2:"+String(distance2);
      Serial.print(sdata);
      delay(100);
      if(op==1){op=0;digitalWrite(9,LOW);}
      break;
    } 
    case '7'://部署
    {
//      digitalWrite(9,HIGH);
//      delay(500);
//      digitalWrite(9,LOW);
//      Serial.println("receive 7");
      op=1;
      myservo.write(100);
      digitalWrite(9,HIGH);
      delay(1000);
      myservo.write(160);
      shut();shut();csb2();shut();shut();
      delay(1000);
      myservo.write(50);
      shut();shut();csb3();shut();shut();
      delay(1000);
      myservo.write(100);
      shut();shut();csb4();shut();shut();
      String sdata2 = "H:"+ String(h) +"\nT:"+ String(t) +"\n"+"D3:"+String(distance3)+"\nD4:"+String(distance4)+"\nD5:"+String(distance5);
//      String sdata2 = "H:"+ String(h) +"\n"+"T:"+ String(t) +"\n"+"D3:"+String(distance3)+"  "+"D4:"+String(distance4)+"  "+"D5:"+String(distance5);
      Serial.print(sdata2);
      delay(1000);
      break;
      
    } 
//    default:
//    {
//      digitalWrite(9,LOW);  //停止
//      digitalWrite(4,LOW);
//      digitalWrite(11,LOW);
//      digitalWrite(12,LOW);
//      delay(0);
//      break;
//    }
  }
  
}
void shut()
{
      digitalWrite(9,HIGH);
      delay(200);
      digitalWrite(9,LOW);
      delay(200);
      digitalWrite(9,HIGH);
      delay(200);
      digitalWrite(9,LOW);
      delay(200);
}

void shut2()
{
      digitalWrite(9,LOW);
      delay(200);
      digitalWrite(9,HIGH);
      delay(200);
      digitalWrite(9,LOW);
      delay(200);
      digitalWrite(9,HIGH);
      delay(200);
      
}
void shut3()
{
      digitalWrite(9,LOW);
      delay(100);
      digitalWrite(9,HIGH);
      delay(100);
      digitalWrite(9,LOW);
      delay(100);
      digitalWrite(9,HIGH);
      delay(100);
      
}
void csb()
{
   digitalWrite(trig,LOW);
   delayMicroseconds(20); 
   digitalWrite(trig,HIGH);
   delayMicroseconds(20);
   digitalWrite(trig,LOW);                     //发一个20US的高脉冲去触发Trig
   
   distance  = pulseIn(echo,HIGH);             //计数接收高电平时间
   distance  = distance*340/2/10000;           //计算距离 1：声速：340M/S  2：实际距离1/2声速距离 3：计数时钟为1US
//   Serial.print("Distance is: ");
//   Serial.print(distance);
//   Serial.print("cm   "); 
   delay(20);                                   //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时，测量到上次余波，导致测量不准确。
//   delay(100);                                  //500mS测量一次
  
   
   digitalWrite(trig2,LOW);
   delayMicroseconds(20); 
   digitalWrite(trig2,HIGH);
   delayMicroseconds(20);
   digitalWrite(trig2,LOW);                     //发一个20US的高脉冲去触发Trig2
   distance2  = pulseIn(echo2,HIGH);             //计数接收高电平时间
   distance2  = distance2*340/2/10000;           //计算距离 1：声速：340M/S  2：实际距离1/2声速距离 3：计数时钟为1US
//   Serial.print("Distance2 is: ");
//   Serial.print(distance2);
//   Serial.println("cm"); 
   delay(20);                                   //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时，测量到上次余波，导致测量不准确。
//   delay(100);                                  //500mS测量一次
  }
  void csb2()
{
   digitalWrite(trig,LOW);
   delayMicroseconds(20); 
   digitalWrite(trig,HIGH);
   delayMicroseconds(20);
   digitalWrite(trig,LOW);                     //发一个20US的高脉冲去触发Trig
   
   distance3  = pulseIn(echo,HIGH);             //计数接收高电平时间
   distance3  = distance3*340/2/10000;           //计算距离 1：声速：340M/S  2：实际距离1/2声速距离 3：计数时钟为1US
//   Serial.print("Distance is: ");
//   Serial.print(distance);
//   Serial.print("cm   "); 
   delay(20);                                   //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时，测量到上次余波，导致测量不准确。

}
  void csb3()
{
   digitalWrite(trig,LOW);
   delayMicroseconds(20); 
   digitalWrite(trig,HIGH);
   delayMicroseconds(20);
   digitalWrite(trig,LOW);                     //发一个20US的高脉冲去触发Trig
   
   distance4  = pulseIn(echo,HIGH);             //计数接收高电平时间
   distance4  = distance4*340/2/10000;           //计算距离 1：声速：340M/S  2：实际距离1/2声速距离 3：计数时钟为1US
//   Serial.print("Distance is: ");
//   Serial.print(distance);
//   Serial.print("cm   "); 
   delay(20);                                   //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时，测量到上次余波，导致测量不准确。
}
  void csb4()
{
   digitalWrite(trig,LOW);
   delayMicroseconds(20); 
   digitalWrite(trig,HIGH);
   delayMicroseconds(20);
   digitalWrite(trig,LOW);                     //发一个20US的高脉冲去触发Trig
   
   distance5  = pulseIn(echo,HIGH);             //计数接收高电平时间
   distance5  = distance5*340/2/10000;           //计算距离 1：声速：340M/S  2：实际距离1/2声速距离 3：计数时钟为1US
//   Serial.print("Distance is: ");
//   Serial.print(distance);
//   Serial.print("cm   "); 
   delay(20);                                   //单次测离完成后加20mS的延时再进行下次测量。防止近距离测量时，测量到上次余波，导致测量不准确。
}
  void dht11q() {
  // Reading temperature or humidity takes about 250 milliseconds!
  h = dht.readHumidity()+19;
  // Read temperature as Celsius (the default)
  t = dht.readTemperature()-3;
  // Read temperature as Fahrenheit (isFahrenheit = true)
  f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

//  Serial.print("Humidityshidu: ");
//  Serial.print(h);
//  Serial.print(" %\t");
//  Serial.print("Temperature: ");
//  Serial.print(t);
//  Serial.println(" *C ");
  
   lcd.setCursor(0,0); 
   
   MyPrintLCD("H:");
   MyPrintLCD(String(h));
   MyPrintLCD(" %");

   lcd.setCursor(0,1); 
   MyPrintLCD("T:");
   MyPrintLCD(String(t));
   MyPrintLCD(" C");
   
}
